package org.example.operation;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.data.category.*;
import org.example.model.*;
import org.example.trans.*;

/**
 * @author lenovo
 */

public class GraphOperation {
    //将courseName对应的courseSegmentScore生成chart并返回，字体大小为wordSize
    public static JFreeChart graphSegmentScore(String courseName,int wordSize){
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ScoreOperation.loadScore();
        ArrayList<Integer> courseSegmentScore = SortOperation.courseSegmentScore(courseName);
        for(int i = 0 ; i < courseSegmentScore.size() ; i++){
            String string = null;
            switch(i){
                case 0:string = "<60";break;
                case 1:string = "60-65";break;
                case 2:string = "65-70";break;
                case 3:string = "70-75";break;
                case 4:string = "75-80";break;
                case 5:string = "80-85";break;
                case 6:string = "85-90";break;
                case 7:string = "90-95";break;
                case 8:string = ">=95";break;
            }
            dataset.addValue(courseSegmentScore.get(i),"",string);
        }
        JFreeChart chart = ChartFactory.createBarChart(null,
                "成绩段","人数",dataset);
        CategoryAxis xAxis = chart.getCategoryPlot().getDomainAxis();
        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
        renderer.setDefaultItemLabelsVisible(true);
        renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setItemLabelAnchorOffset(-2);
        renderer.setDefaultItemLabelFont(new Font("SimSun",Font.PLAIN,wordSize));
        xAxis.setLabelFont(new Font("SimSun",Font.PLAIN,wordSize));
        xAxis.setTickLabelFont(new Font("SimSun",Font.PLAIN,wordSize));
        ValueAxis yAxis = chart.getCategoryPlot().getRangeAxis();
        yAxis.setLabelFont(new Font("SimSun",Font.PLAIN,wordSize));
        yAxis.setTickLabelFont(new Font("SimSun",Font.PLAIN,wordSize));
        return chart;
    }
    //生成table组件数据的tableModel并返回
    public static DefaultTableModel outTable(){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        ArrayList<ArrayList<String>> arrayListOut = TransSort.transSortWrite
                (Save.getHashMapCourse(), SortOperation.sortNum());
        Object[] course = arrayListOut.get(0).toArray();
        Object[][] score = new Object[arrayListOut.size()-1][];
        for(int i = 1 ; i < arrayListOut.size() ; i++){
            score[i-1] = arrayListOut.get(i).toArray();
        }
        return new DefaultTableModel(score,course);
    }
    //生成table组件报表的tableModel并返回
    public static DefaultTableModel totalTable(){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        ArrayList<ArrayList<String>> arrayListOut = TransSort.transSortWriteTotal
                (Save.getHashMapCourse(), SortOperation.sortScore(),
                        SortOperation.averageCourseScore());
        Object[] course = arrayListOut.get(0).toArray();
        Object[][] score = new Object[arrayListOut.size()-1][];
        for(int i = 1 ; i < arrayListOut.size() ; i++){
            score[i-1] = arrayListOut.get(i).toArray();
        }
        return new DefaultTableModel(score,course);
    }
}
